package em.util;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;

import java.util.zip.CRC32;

public class TermUtil {
	public static Integer TermCodeXOR(String termCode) {
		if(StrKit.isBlank(termCode) || !termCode.toLowerCase().contains("pt")){
			return 0;
		}
		Integer tnum = Integer.valueOf(termCode.substring(2)) ^ 0x5A5A5A5A;
		return tnum;
	}
	public static long CRC32(byte[] filecontent) {
		CRC32 crc32 = new CRC32();
		crc32.update(filecontent);
		System.out.println(crc32.getValue());
		return crc32.getValue();
	}

	/**
	 *
	 * @param LitDoor
	 * @param LitHalf
	 * @param DefaultValue
	 * @param TriggerValue  限位颜色
	 * @param NoTriggerValue 未限位颜色
	 * @param Language
     * @return
     */
	public static JSONObject LimitAnalysis(int LitDoor,int LitHalf,String DefaultValue,String TriggerValue,String NoTriggerValue,String Language){
		JSONObject json=new JSONObject();
		String[] dStatusArr={DefaultValue,DefaultValue,DefaultValue,DefaultValue,DefaultValue,DefaultValue,DefaultValue,DefaultValue};
		String[] hStatusArr={DefaultValue,DefaultValue,DefaultValue,DefaultValue,DefaultValue,DefaultValue,DefaultValue,DefaultValue};
		String[] dNameArr;
		String[] hNameArr;
		if("en".equals(Language.toLowerCase())){
			dNameArr=new String[]{"SimpleDoor","DoubleDoor","TopDoor"};
			hNameArr=new String[]{"DownLit","UpLit"};
		}else{
			dNameArr=new String[]{"单门","双门","顶门"};
			hNameArr=new String[]{"下限位","上限位"};
		}
		if(LitDoor!=255){
			String ldStr = String.format("%08d", Integer.valueOf(Integer.toBinaryString(LitDoor)));
			for(int i=0;i<ldStr.getBytes().length;i++){
				if(LimitConvert(LitDoor,i)==1){
					dStatusArr[i]=TriggerValue;
				}else if(LimitConvert(LitDoor,i)==0){
					dStatusArr[i]=NoTriggerValue;
				}
			}
		}
		if(LitHalf!=255) {
			String lhStr = String.format("%08d", Integer.valueOf(Integer.toBinaryString(LitHalf)));
			for (int i = 0; i < lhStr.getBytes().length; i++) {
				if (LimitConvert(LitHalf, i) == 1) {
					hStatusArr[i] = TriggerValue;
				} else if (LimitConvert(LitHalf, i) == 0) {
					hStatusArr[i] = NoTriggerValue;
				}
			}
		}
		for(int i=0;i<dNameArr.length;i++){
			json.put(dNameArr[i], dStatusArr[i]);
		}
		for(int i=0;i<hNameArr.length;i++){
			json.put(hNameArr[i], hStatusArr[i]);
		}
		return json;
	}
	public static int  LimitConvert(int Lit,int index){
		return (Lit & (0x1 << index)) >> index;
	}
}